{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# construct input for tool B and C from tool A's output\n",
    "folder_name_A = '/Users/kavyasrinet/Desktop/other_actions/5/toolA/'\n",
    "folder_name_B = '/Users/kavyasrinet/Desktop/other_actions/5/toolB/'\n",
    "folder_name_C = '/Users/kavyasrinet/Desktop/other_actions/5/toolC/'\n",
    "# folder_name_1 = '/Users/kavyasrinet/Downloads/'\n",
    "# folder_name_2 = folder_name_1\n",
    "tool_A_out_file = folder_name_A + 'all_agreements.txt'\n",
    "tool_B_input_file = folder_name_B + 'input.txt'\n",
    "tool_C_input_file = folder_name_C + 'input.txt'\n",
    "\n",
    "from pprint import pprint\n",
    "'''\n",
    "command: Input.command\n",
    "\n",
    "'''\n",
    "import ast\n",
    "import json\n",
    "import copy\n",
    "\n",
    "example_d = {}\n",
    "\n",
    "def merge_indices(indices):\n",
    "    a, b = indices[0]\n",
    "    for i in range(1, len(indices)):\n",
    "        x, y = indices[i]\n",
    "        if x != b + 1:\n",
    "            return indices\n",
    "        else:\n",
    "            b = y\n",
    "    if a==b:\n",
    "        return a\n",
    "    return [a, b]\n",
    "\n",
    "\n",
    "all_actions = {}\n",
    "composites= []\n",
    "with open(tool_A_out_file, \"r\") as f1, open(tool_B_input_file, 'w') as f2, open(tool_C_input_file, 'w') as f3:\n",
    "    \n",
    "    for line in f1.readlines():\n",
    "        line  = line.strip()\n",
    "        cmd, out = line.split(\"\\t\")\n",
    "        words = cmd.split()\n",
    "        \n",
    "        words_copy = copy.deepcopy(words)\n",
    "        action_dict = ast.literal_eval(out)\n",
    "        action_type = action_dict['action_type'][1]\n",
    "        \n",
    "        all_actions[action_type] =  all_actions.get(action_type, 0)+ 1\n",
    "        # write composite separately\n",
    "        if action_type=='composite_action':\n",
    "            composites.append(\" \".join(words))\n",
    "            continue\n",
    "         \n",
    "        # no need to annotate children of these two actions\n",
    "        if action_type == 'noop':\n",
    "            continue\n",
    "            \n",
    "        # find children that need to be re-annotated\n",
    "        for key, val in action_dict.items():\n",
    "            child_name = None\n",
    "            # child needs annotation\n",
    "            if val[0]== 'no':\n",
    "                # insert \"span\"\n",
    "                words_copy = copy.deepcopy(words)\n",
    "                child_name = key\n",
    "                \n",
    "                write_line = \"\"\n",
    "                write_line += \" \".join(words) + \"\\t\"\n",
    "                #print(words, child_name, action_type, val[1])\n",
    "                \n",
    "                indices = merge_indices(val[1])\n",
    "                span_text = None\n",
    "                if type(indices) == list:\n",
    "                    if type(indices[0]) == list:\n",
    "                        # this means that indices were scattered and disjoint\n",
    "                        for idx in indices:\n",
    "                            words_copy[idx[0]] = \"<span style='background-color: #FFFF00'>\" + words_copy[idx[0]]\n",
    "                            words_copy[idx[1]] = words_copy[idx[1]] + \"</span>\"\n",
    "                    else:\n",
    "                        words_copy[indices[0]] = \"<span style='background-color: #FFFF00'>\" + words_copy[indices[0]]\n",
    "                        words_copy[indices[1]] = words_copy[indices[1]] + \"</span>\"\n",
    "                else:\n",
    "                    words_copy[indices] = \"<span style='background-color: #FFFF00'>\" + words_copy[indices] + \"</span>\"\n",
    "                write_line += \" \".join(words_copy) + \"\\t\" + action_type + \"\\t\" + child_name\n",
    "                \n",
    "                if child_name in ['reference_object', \n",
    "                                  'receiver_reference_object', \n",
    "                                  'source_reference_object']:\n",
    "                    # write for tool C \n",
    "                    f3.write(write_line+ \"\\n\")\n",
    "                else:\n",
    "                    # write for tool B\n",
    "                    f2.write(write_line+ \"\\n\")\n",
    "                #print(write_line)\n",
    "        \n",
    "        #print(\"----\"*5)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# write out all unique composites\n",
    "# only needs to be done once\n",
    "mypath = '../../minecraft/python/craftassist/text_to_tree_tool/turk_data/tool1/humanbot/'\n",
    "from os import walk\n",
    "import ast\n",
    "\n",
    "#all_composites = set()\n",
    "\n",
    "f = []\n",
    "for (dirpath, dirnames, filenames) in walk(mypath):\n",
    "    if dirnames:\n",
    "        #int(dirnames, dirpath)\n",
    "        for dirname in dirnames:\n",
    "            print(\"'\" + dirname+\"'\")\n",
    "            folder = str(dirpath) + dirname + \"/\" \n",
    "            file_name = folder + \"all_agreements.txt\"\n",
    "            with open(file_name) as f:\n",
    "                #print(file_name)\n",
    "                for line in f.readlines():\n",
    "                    line = line.strip()\n",
    "                    text, d = line.split(\"\\t\")\n",
    "                    action_type = ast.literal_eval(d)['action_type'][1]\n",
    "                    if action_type == 'composite_action':\n",
    "                        all_composites.add(text.strip())\n",
    "print(len(all_composites))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# now write composites to a file\n",
    "with open('../../minecraft/python/craftassist/text_to_tree_tool/turk_data/all_composites.txt', 'w') as f:\n",
    "    for c in all_composites:\n",
    "        f.write(c + \"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# construct input for tool D from tool C's output\n",
    "# inpout for tool D is : \n",
    "# text \\t highlighted_text \\t \"reference_object\" \\t \"comparison\"\n",
    "# folder_name_C = '../../minecraft/python/craftassist/text_to_tree_tool/turk_data/new_dance_form_data/next_20/tool3/'\n",
    "# folder_name_D = '../../minecraft/python/craftassist/text_to_tree_tool/turk_data/new_dance_form_data/next_20/tool4/'\n",
    "folder_name_C = '/Users/kavyasrinet/Desktop/other_actions/0/toolC/'\n",
    "folder_name_D = '/Users/kavyasrinet/Desktop/other_actions/0/toolD/'\n",
    "tool_C_out_file = folder_name_C + 'all_agreements.txt'\n",
    "tool_D_input_file = folder_name_D + 'input.txt'\n",
    "\n",
    "from pprint import pprint\n",
    "'''\n",
    "command: Input.command\n",
    "\n",
    "'''\n",
    "import copy\n",
    "example_d = {}\n",
    "\n",
    "def merge_indices(indices):\n",
    "    a, b = indices[0]\n",
    "    for i in range(1, len(indices)):\n",
    "        x, y = indices[i]\n",
    "        if x != b + 1:\n",
    "            return indices\n",
    "            # raise NotImplementedError(\"Spans not continuous during merging!!!\")\n",
    "        else:\n",
    "            b = y\n",
    "    if a==b:\n",
    "        return a\n",
    "    return [a, b]\n",
    "import ast\n",
    "import json\n",
    "\n",
    "\n",
    "all_actions = {}\n",
    "with open(tool_C_out_file, \"r\") as f1, open(tool_D_input_file, 'w') as f2:\n",
    "    composites= []\n",
    "    \n",
    "    for line in f1.readlines():\n",
    "        line  = line.strip()\n",
    "        cmd, action_child_name , out = line.split(\"\\t\")\n",
    "        words = cmd.split()\n",
    "        \n",
    "        words_copy = copy.deepcopy(words)\n",
    "        #pprint(out)\n",
    "        if action_child_name not in ['reference_object', \n",
    "                                     'receiver_reference_object',\n",
    "                                     'source_reference_object']:\n",
    "            print(\"Not a reference object!!!\")\n",
    "            continue\n",
    "        out_dict = json.loads(out)\n",
    "        ref_obj_dict = out_dict[action_child_name]\n",
    "#         print(out_dict)\n",
    "#         print(ref_obj_dict)\n",
    "#         break\n",
    "            \n",
    "        # find children that need to be re-annotated\n",
    "        for key, val in ref_obj_dict.items():\n",
    "            child_name = None\n",
    "            # child needs annotation\n",
    "            if val[0]== 'no':\n",
    "                # insert \"span\"\n",
    "                words_copy = copy.deepcopy(words)\n",
    "                child_name = key\n",
    "                \n",
    "                write_line = \"\"\n",
    "                write_line += \" \".join(words) + \"\\t\"\n",
    "                #print(words, child_name, action_type, val[1])\n",
    "                \n",
    "                indices = merge_indices(val[1])\n",
    "                span_text = None\n",
    "                if type(indices) == list:\n",
    "                    if type(indices[0]) == list:\n",
    "                        # this means that indices were scattered and disjoint\n",
    "                        for idx in indices:\n",
    "                            words_copy[idx[0]] = \"<span style='background-color: #FFFF00'>\" + words_copy[idx[0]]\n",
    "                            words_copy[idx[1]] = words_copy[idx[1]] + \"</span>\"\n",
    "                    else:\n",
    "                        words_copy[indices[0]] = \"<span style='background-color: #FFFF00'>\" + words_copy[indices[0]]\n",
    "                        words_copy[indices[1]] = words_copy[indices[1]] + \"</span>\"\n",
    "                else:\n",
    "                    words_copy[indices] = \"<span style='background-color: #FFFF00'>\" + words_copy[indices] + \"</span>\"\n",
    "                write_line += \" \".join(words_copy) + \"\\t\" + action_child_name + \"\\t\" + child_name\n",
    "                f2.write(write_line+ \"\\n\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
